package com.chy.edu.controller;


import com.chy.commonutils.Result;
import com.chy.edu.pojo.EduChapter;
import com.chy.edu.pojo.chapter.ChapterVo;
import com.chy.edu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author chy
 * @since 2020-10-27
 */
@Api(tags = "课程章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @ApiOperation(value = "课程大纲列表，根据课程id进行查询")
    @GetMapping("getChapterVideo/{courseId}")
    public Result getChapterVideo(@ApiParam(name = "courseId", value = "课程ID", required = true)
                                      @PathVariable String courseId)
    {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return Result.ok()
                .data("allChapterVideo",list);
    }

    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public Result addChapter(@ApiParam(name = "eduChapter", value = "章节对象", required = true)
                                 @RequestBody EduChapter eduChapter)
    {
        chapterService.save(eduChapter);
        return Result.ok();
    }

    @ApiOperation(value = "根据章节id查询")
    @GetMapping("getChapterInfo/{chapterId}")
    public Result getChapterInfo(@ApiParam(name = "chapterId", value = "章节ID", required = true)
                                     @PathVariable String chapterId)
    {
        EduChapter eduChapter = chapterService.getById(chapterId);
        return Result.ok().data("chapter",eduChapter);
    }

    @ApiOperation(value = "修改章节")
    @PostMapping("updateChapter")
    public Result updateChapter(@ApiParam(name = "eduChapter", value = "章节对象", required = true)
                                    @RequestBody EduChapter eduChapter)
    {
        chapterService.updateById(eduChapter);
        return Result.ok();
    }

    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("{chapterId}")
    public Result deleteChapter(@ApiParam(name = "id", value = "章节ID", required = true)
                                    @PathVariable String chapterId)
    {
        return chapterService.deleteChapter(chapterId) ? Result.ok() : Result.error();
    }
}

